import request, { post, get, put, del } from '../../utils/request.js';
import { showToast } from '../../utils/common.js';

const state = {
  cartList: []
};

const mutations = {
  SET_CART_LIST(state, list) {
    state.cartList = list;
  },
  UPDATE_CART_ITEM(state, { id, count, selected }) {
    const item = state.cartList.find(item => item.id === id);
    if (item) {
      if (count !== undefined) {
        item.count = count;
      }
      if (selected !== undefined) {
        item.selected = selected;
      }
    }
  },
  REMOVE_CART_ITEM(state, id) {
    const index = state.cartList.findIndex(item => item.id === id);
    if (index !== -1) {
      state.cartList.splice(index, 1);
    }
  },
  CLEAR_CART(state) {
    state.cartList = [];
  }
};

const actions = {
  // 获取购物车列表
  async getCartList({ commit }) {
    try {
      const res = await get('/api/cart/list');
      commit('SET_CART_LIST', res);
      return res;
    } catch (error) {
      showToast('获取购物车失败');
      return [];
    }
  },
  
  // 添加商品到购物车
  async addToCart({ commit, dispatch }, params) {
    try {
      const res = await post('/api/cart/add', params);
      showToast('添加成功', 'success');
      // 刷新购物车列表
      dispatch('getCartList');
      return res;
    } catch (error) {
      showToast('添加失败，请重试');
      return null;
    }
  },
  
  // 更新购物车商品
  async updateCartItem({ commit }, params) {
    try {
      const res = await put('/api/cart/update', params);
      commit('UPDATE_CART_ITEM', params);
      return res;
    } catch (error) {
      showToast('更新失败，请重试');
      return null;
    }
  },
  
  // 删除购物车商品
  async removeCartItem({ commit }, id) {
    try {
      const res = await del(`/api/cart/remove/${id}`);
      commit('REMOVE_CART_ITEM', id);
      showToast('删除成功', 'success');
      return true;
    } catch (error) {
      showToast('删除失败，请重试');
      return false;
    }
  },
  
  // 全选/取消全选
  async selectAll({ commit, dispatch }, selected = true) {
    try {
      const res = await put('/api/cart/select-all', { selected });
      // 刷新购物车列表
      dispatch('getCartList');
      return true;
    } catch (error) {
      showToast('操作失败，请重试');
      return false;
    }
  },
  
  // 清空购物车
  async clearCart({ commit }) {
    try {
      const res = await del('/api/cart/clear');
      commit('CLEAR_CART');
      return true;
    } catch (error) {
      showToast('清空购物车失败');
      return false;
    }
  }
};

const getters = {
  cartList: state => state.cartList,
  cartCount: state => state.cartList.reduce((total, item) => total + item.count, 0),
  selectedCartList: state => state.cartList.filter(item => item.selected),
  selectedCount: state => state.cartList.filter(item => item.selected).reduce((total, item) => total + item.count, 0),
  totalPrice: state => state.cartList.filter(item => item.selected).reduce((total, item) => total + item.price * item.count, 0),
  isAllSelected: state => state.cartList.length > 0 && state.cartList.every(item => item.selected)
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}; 